package com.jgsu.farm.controller;

import com.jgsu.farm.entity.*;
import com.jgsu.farm.exception.*;
import com.jgsu.farm.service.InformationService;
import com.jgsu.farm.utils.CommonResult;
import com.jgsu.farm.utils.ResultCode;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(value = "farm/sysuser/information", tags = "胁迫情报管理模块")  //描述信息用的，标注在类上
@RestController
@RequestMapping("farm/sysuser/information")
public class InformationController {
    @Autowired
    InformationService informationService;


    /**
     * 胁迫情报增加
     */
    @ApiOperation(value = "胁迫情报添加功能")
    @PostMapping(value = "/add", produces = "application/json")
    public CommonResult informationAdd(Information information) {
        try {
            informationService.add(information);
            log.info("胁迫情报添加成功，title为：{}", information.getTitle());
            return CommonResult.success(information);
        } catch (DataAddException e) {
            log.error("胁迫情报添加异常,title为：{}", information.getTitle());
            throw new DataAddException(e.getCode(), e.getMessage());
        }
    }

    /**
     * 胁迫情报删除
     */
    @ApiOperation(value = "胁迫情报删除功能")
    @DeleteMapping(value = "/delete", produces = "application/json")
    public CommonResult informationDelete(@RequestParam("infoid") Integer infoid) {
        try {
            informationService.delete(infoid);
            log.info("删除id为：" + infoid + "的胁迫情报信息");
            return CommonResult.success(ResultCode.SUCCESS);
        } catch (DataNotExistException dde) {
            log.error("不存在id为：" + infoid + "的胁迫情报，删除失败");
            throw new DataNotExistException(dde.getCode(), dde.getMessage());
        }
    }

    /**
     * 胁迫情报查询
     */
    @ApiOperation(value = "胁迫情报查询功能")
    @GetMapping(produces = "application/json")
    public CommonResult<PageInfo<Information>> informationList(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                                               @RequestParam(value = "pagesize", required = false, defaultValue = "10") int pagesize) {
        try {
            PageInfo<Information> informationList = informationService.selectAll(page, pagesize);
            return CommonResult.success(informationList);
        } catch (DataNotExistException e) {
            log.error("胁迫情报表中没有数据");
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }

    /**
     * 胁迫情报修改
     */
    @ApiOperation(value = "胁迫情报修改功能")
    @PutMapping(value = "/update", produces = "application/json")
    public CommonResult informationUpdate(Information information) {
        try {
            informationService.update(information);
            log.info("胁迫情报更新，更新id为：{}", information.getInfoid());
            return CommonResult.success(information);
        } catch (DataNotExistException due) {
            log.error("更新id:{}的胁迫情报失败", information.getInfoid());
            throw new DataNotExistException(due.getCode(), due.getMessage());
        }
    }
}
